<template>
  <main class="main-page">
    <BasicForm @register="register" />
    <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleAdd">新增</a-button>
    <section class="list">
      <section class="card" v-for="(item, index) in dataSources" :key="index">
        <div
          class="top"
          v-for="(i, idx) in sources.filter((k) => k.name.toLowerCase() == item.source.toLowerCase())"
          :key="idx"
          :style="{ background: i.bkground }"
        >
          <img :src="i.img" />
          <span class="type">{{ item.source }}</span>
        </div>
        <div class="name">
          <Icon class="card-icon-check" icon="ant-design:check-circle-outlined" v-if="item.state" />
          <Icon class="card-icon-close" icon="ant-design:close-circle-outlined" v-else />
          <span>{{ item.sourceName }}</span>
        </div>

        <div class="bottom">
          <span class="time"> {{ item.createTime }} </span>
          <div class="btns">
            <img
              src=""
              class="row-btn-img"
              @click="handleEdit(item)"
            />
            <img
              src=""
              class="row-btn-img"
              @click="handleDetail(item)"
            />
            <a-popconfirm title="您确定要删除吗？" ok-text="确定" cancel-text="取消" @confirm="handleDelete(item)">
              <img
                src=""
                class="row-btn-img"
              />
            </a-popconfirm>
          </div>
        </div>
      </section>
    </section>
  </main>
  <DataSourceModal @register="registerModal" @success="reload" />
  <DataSourceDrawer @register="registerDrawer" @success="handleSuccess" />
</template>
<script lang="ts" setup>
  import dataSources from './dataSource.json';
  import DataSourceModal from './DataSourceModal.vue';
  import DataSourceDrawer from './DataSourceDrawer.vue';
  import { searchFormSchema, sources } from './dataSource.data';
  import { list, deleteRequest } from './dataSource.api';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { useModal } from '/@/components/Modal';
  import { useDrawer } from '/@/components/Drawer';
  const [registerModal, { openModal }] = useModal();
  const [registerDrawer, { openDrawer }] = useDrawer();
  const [register] = useForm({
    labelWidth: 80,
    schemas: searchFormSchema,
    actionColOptions: {
      span: 6,
    },
  });

  function handleAdd() {
    openModal(true, {
      isUpdate: false,
      showFooter: true,
    });
  }

  function handleEdit(record) {
    openDrawer(true, {
      record,
      isUpdate: true,
      showFooter: true,
    });
  }

  function handleDetail(record) {
    openDrawer(true, {
      record,
      isUpdate: true,
      showFooter: false,
    });
  }

  async function handleDelete(record) {
    await deleteRequest({ id: record.id });
  }
</script>
<style scoped>
  .main-page {
    padding: 20px;
    background-color: #fff;
  }
  .main-page .list {
    margin-top: 20px;
    display: flex;
    flex-wrap: wrap;
  }
  .main-page .list .card {
    flex: 0 0 280px;
    margin: 0 16px 8px 0;
    background: #fff;
    border-radius: 10px;
    border: 1px solid #efefef;
    height: 205px;
    overflow: hidden;
  }
  .list .card .top {
    padding: 41px 0 41px 40px;
    border-bottom: 1px solid #dce4f2;
  }
  .list .card .top img {
    width: 50px;
    height: 50px;
    vertical-align: top;
  }
  .list .card .top .type {
    margin-left: 18px;
    font-size: 22px;
    font-weight: 400;
    color: rgba(61, 61, 61, 0.7);
    line-height: 50px;
  }
  .list .card .name {
    margin-top: 10px;
    color: #3d3d3d;
    line-height: 16px;
    padding: 0 18px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .list .card .name .card-icon-close {
    color: red;
    margin-right: 15px;
  }
  .list .card .name .card-icon-check {
    color: green;
    margin-right: 15px;
  }
  .list .card .bottom {
    padding: 7px 18px;
    display: flex;
    justify-content: space-between;
  }
  .list .card .bottom .time {
    font-size: 10px;
    color: #3d3d3d;
    line-height: 24px;
  }
  .row-btn-img {
    width: 24px;
    height: 24px;
    margin-right: 10px;
    cursor: pointer;
  }
</style>
